package com.ruoyi.ldzlsb.service.impl;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ldzlsb.mapper.SbMaintenancePlanMapper;
import com.ruoyi.ldzlsb.domain.SbMaintenancePlan;
import com.ruoyi.ldzlsb.domain.SbMaintenanceRecord;
import com.ruoyi.ldzlsb.service.ISbMaintenancePlanService;
import com.ruoyi.common.core.exception.ServiceException;

/**
 * 保养计划Service业务层处理
 * 
 * @author anli
 * @date 2025-01-11
 */
@Service
public class SbMaintenancePlanServiceImpl implements ISbMaintenancePlanService 
{
    @Autowired
    private SbMaintenancePlanMapper sbMaintenancePlanMapper;

    /**
     * 查询保养计划
     * 
     * @param planId 保养计划主键
     * @return 保养计划
     */
    @Override
    public SbMaintenancePlan selectSbMaintenancePlanByPlanId(Long planId)
    {
        return sbMaintenancePlanMapper.selectSbMaintenancePlanByPlanId(planId);
    }

    /**
     * 查询保养计划列表
     * 
     * @param sbMaintenancePlan 保养计划
     * @return 保养计划
     */
    @Override
    public List<SbMaintenancePlan> selectSbMaintenancePlanList(SbMaintenancePlan sbMaintenancePlan)
    {
        return sbMaintenancePlanMapper.selectSbMaintenancePlanList(sbMaintenancePlan);
    }

    /**
     * 查询保养计划列表（包含关联信息）
     * 
     * @param sbMaintenancePlan 保养计划
     * @return 保养计划集合
     */
    @Override
    public List<SbMaintenancePlan> selectSbMaintenancePlanWithInfoList(SbMaintenancePlan sbMaintenancePlan)
    {
        return sbMaintenancePlanMapper.selectSbMaintenancePlanWithInfoList(sbMaintenancePlan);
    }

    /**
     * 新增保养计划
     * 
     * @param sbMaintenancePlan 保养计划
     * @return 结果
     */
    @Override
    public int insertSbMaintenancePlan(SbMaintenancePlan sbMaintenancePlan)
    {
        sbMaintenancePlan.setCreateTime(new Date());
        if (sbMaintenancePlan.getIsDelete() == null)
        {
            sbMaintenancePlan.setIsDelete("0");
        }
        if (sbMaintenancePlan.getPlanStatus() == null)
        {
            sbMaintenancePlan.setPlanStatus("1");
        }
        if (sbMaintenancePlan.getAutoGenerate() == null)
        {
            sbMaintenancePlan.setAutoGenerate("1");
        }
        return sbMaintenancePlanMapper.insertSbMaintenancePlan(sbMaintenancePlan);
    }

    /**
     * 修改保养计划
     * 
     * @param sbMaintenancePlan 保养计划
     * @return 结果
     */
    @Override
    public int updateSbMaintenancePlan(SbMaintenancePlan sbMaintenancePlan)
    {
        sbMaintenancePlan.setUpdateTime(new Date());
        return sbMaintenancePlanMapper.updateSbMaintenancePlan(sbMaintenancePlan);
    }

    /**
     * 批量删除保养计划
     * 
     * @param planIds 需要删除的保养计划主键
     * @return 结果
     */
    @Override
    public int deleteSbMaintenancePlanByPlanIds(Long[] planIds)
    {
        return sbMaintenancePlanMapper.deleteSbMaintenancePlanByPlanIds(planIds);
    }

    /**
     * 删除保养计划信息
     * 
     * @param planId 保养计划主键
     * @return 结果
     */
    @Override
    public int deleteSbMaintenancePlanByPlanId(Long planId)
    {
        return sbMaintenancePlanMapper.deleteSbMaintenancePlanByPlanId(planId);
    }

    /**
     * 检查保养计划编号是否唯一
     * 
     * @param sbMaintenancePlan 保养计划
     * @return 结果
     */
    @Override
    public boolean checkPlanCodeUnique(SbMaintenancePlan sbMaintenancePlan)
    {
        SbMaintenancePlan plan = sbMaintenancePlanMapper.checkPlanCodeUnique(sbMaintenancePlan.getPlanCode());
        if (plan == null)
        {
            return true;
        }
        if (sbMaintenancePlan.getPlanId() == null)
        {
            return false;
        }
        return plan.getPlanId().equals(sbMaintenancePlan.getPlanId());
    }

    /**
     * 生成保养计划编号
     * 
     * @return 保养计划编号
     */
    @Override
    public String generatePlanCode()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        int todayCount = sbMaintenancePlanMapper.countTodayMaintenancePlans();
        int count = todayCount + 1;
        String countStr = String.format("%03d", count);
        return "BYPJ" + dateStr + countStr;
    }

    /**
     * 根据设备ID查询保养计划
     * 
     * @param equipmentId 设备ID
     * @return 保养计划列表
     */
    @Override
    public List<SbMaintenancePlan> selectSbMaintenancePlanByEquipmentId(Long equipmentId)
    {
        return sbMaintenancePlanMapper.selectSbMaintenancePlanByEquipmentId(equipmentId);
    }

    /**
     * 根据保养人员ID查询保养计划
     * 
     * @param maintainerId 保养人员ID
     * @return 保养计划列表
     */
    @Override
    public List<SbMaintenancePlan> selectSbMaintenancePlanByMaintainerId(Long maintainerId)
    {
        return sbMaintenancePlanMapper.selectSbMaintenancePlanByMaintainerId(maintainerId);
    }

    /**
     * 根据配置ID查询保养计划
     * 
     * @param configId 配置ID
     * @return 保养计划列表
     */
    @Override
    public List<SbMaintenancePlan> selectSbMaintenancePlanByConfigId(Long configId)
        {
        return sbMaintenancePlanMapper.selectSbMaintenancePlanByConfigId(configId);
    }

    /**
     * 根据保养计划创建保养记录
     * 
     * @param plan 保养计划
     * @return 保养记录
     */
    @Override
    public SbMaintenanceRecord createMaintenanceRecordFromPlan(SbMaintenancePlan plan)
    {
        if (plan == null)
        {
            throw new ServiceException("保养计划不能为空");
        }

        SbMaintenanceRecord record = new SbMaintenanceRecord();
        record.setPlanId(plan.getPlanId());
        record.setConfigId(plan.getConfigId());
        record.setConfigName(plan.getConfigName());
        record.setEquipmentId(plan.getEquipmentId());
        record.setEquipmentName(plan.getEquipmentName());
        record.setMaintainerId(plan.getMaintainerId());
        record.setMaintainerName(plan.getMaintainerName());
        record.setDeptId(plan.getDeptId());
        record.setDeptName(plan.getDeptName());
        record.setMaintenanceDate(new Date());
        record.setMaintenanceStatus("0"); // 未开始
        record.setMaintenanceResult("2"); // 待处理
        record.setIsDelete("0");
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());

        // 生成记录编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        String recordCode = "BYJL" + dateStr + String.format("%04d", (int)(Math.random() * 10000));
        record.setRecordCode(recordCode);

        return record;
    }
} 